<template>
	<view>
		<!-- 头部 -->
		<myHead :title="pageTitle"></myHead>

		<uni-search-bar v-model="searchKeyword" @confirm="performSearch" @input="fetchSearchSuggestions()" @cancel="toggleSearchHistory()"
			class="search" :radius="20"></uni-search-bar>

		<!-- 搜索历史 -->
		<view v-if="searchHistory.length > 0 && showSearchHistory" class="search-history-container">
			<text class="history-title">搜索历史</text>
			<text @click="clearSearchHistory" class="iconfont icon-delete"></text>
			<view v-for="(keyword, index) in searchHistory" :key="index" @click="selectHistoryItem(keyword)"
				class="history-item">
				<text>{{ keyword }}</text>
			</view>
		</view>

		<!-- 热搜 -->
		<view v-if="hotKeywords.length > 0 && searchKeyword === ''" class="hot-container">
			<text>热搜榜</text>
			<view v-for="(keyword, index) in hotKeywords" :key="index" @click="searchHotKeyword(keyword)" class="hot-item">
				<text>{{index+1}}.{{ keyword }}</text>
			</view>
		</view>

		<!-- 搜索建议 -->
		<view v-if="searchSuggestions.length > 0 && searchKeyword !== '' && !isSearching " class="search-suggestions-container">
			<text>搜索'{{searchKeyword}}'</text>
			<view v-for="(keyword, index) in searchSuggestions" :key="index" @click="selectSuggestion(keyword)"
				class="suggestion-item">
				<text>{{ keyword }}</text>
			</view>
		</view>

		<!-- 搜索结果展示区域 -->
		<view v-if="searchResults.length > 0 && searchKeyword !== '' && isSearching" class="search-results-container">
			<text>搜索结果</text>
			<view v-for="(result, index) in searchResults" :key="index" @click="playSelectedSong(result.id)" class="result-item">
				<text>{{ result.name }}</text>
				<text class="iconfont icon-play"></text>
			</view>
		</view>
	</view>
</template>

<script>
	import myHead from '../../components/myHead/myHead.vue'
	import { searchHot, searchWord, searchSuggest } from '../../commom/api.js'

	export default {
		data() {
			return {
				pageTitle: '搜索',
				searchKeyword: '',
				hotKeywords: [],
				searchSuggestions: [],
				searchResults: [],
				isSearching: null,
				searchHistory: [],
				showSearchHistory: false
			};
		},
		methods: {
			async fetchHotKeywords() {
				try {
					const res = await searchHot();
					this.hotKeywords = res.data.data.map(item => item.searchWord);
				} catch (error) {
					console.error('获取热搜词出错：', error);
				}
			},
			async fetchSearchSuggestions() {
				try {
					const res = await searchSuggest(this.searchKeyword);
					this.searchSuggestions = res.data.result.allMatch.map(item => item.keyword);
					this.isSearching = false;
					this.showSearchHistory = false;
				} catch (error) {
					console.error('获取搜索建议出错：', error);
				}
			},
			async performSearch() {
				try {
					const res = await searchWord(this.searchKeyword);
					this.searchResults = res.data.result.songs;
					this.isSearching = true;
					this.showSearchHistory = false;
					if (this.searchKeyword.trim() !== '') {
						this.addToSearchHistory(this.searchKeyword.trim());
					}
				} catch (error) {
					console.error('搜索出错：', error);
				}
			},
			playSelectedSong(id) {
				uni.navigateTo({
					url: '/pages/player/player?songId=' + id
				});
			},
			searchHotKeyword(keyword) {
				this.searchKeyword = keyword;
				this.performSearch();
			},
			addToSearchHistory(keyword) {
				if (!this.searchHistory.includes(keyword)) {
					this.searchHistory.unshift(keyword);
				}
			},
			selectHistoryItem(keyword) {
				this.searchKeyword = keyword;
				this.performSearch();
			},
			selectSuggestion(keyword) {
				this.searchKeyword = keyword;
				this.performSearch();
			},
			clearSearchHistory() {
				this.searchHistory = [];
			},
			toggleSearchHistory() {
				this.showSearchHistory = !this.showSearchHistory;
			}
		},
		mounted() {
			this.fetchHotKeywords();
		},
	};
</script>

	<style scoped>
		.search-history-container {
			display: block;
			font-size: 28rpx;
			margin-left: 30rpx;
		}
	
		.history-item {
			display: inline-block;
			font-size: 22rpx;
			background-color: #e4e4e4;
			padding: 15rpx;
			margin: 10rpx 5rpx 10rpx 30rpx;
			border-radius: 30rpx;
		}
	
		.icon-delete {
			position: relative;
			bottom: 30rpx;
			left: 530rpx;
		}
	
		.hot-container {
			margin-left: 30rpx;
			padding: 10rpx;
			background-color: white;
			width: 320rpx;
			border-radius: 10rpx;
		}
	
		.hot-item {
			font-size: 28rpx;
			margin: 10rpx 0;
		}
	
		.search-suggestions-container {
			margin-left: 30rpx;
			padding: 10rpx;
			background-color: white;
			width: 320rpx;
			border-radius: 10rpx;
		}
	
		.suggestion-item {
			font-size: 28rpx;
			margin: 10rpx 0;
		}
	
		.search-results-container {
			margin-left: 30rpx;
			padding: 10rpx;
			background-color: white;
			border-radius: 10rpx;
		}
	
		.result-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			padding: 10rpx 20rpx;
		}
	
		.iconfont {
			color: #999;
		}
	</style>